# -*- coding: utf-8 -*-
from tkinter import *
from tkinter import messagebox
import csv

root = Tk(className='招标评分器')

Label(text="投标对象个数").grid(row=0, column=0)
Label(text="评委人数").grid(row=1, column=0)

textfiled1 = Entry(width=5)
textfiled2 = Entry(width=5)
textfiled1.grid(row=0, column=1)
textfiled2.grid(row=1, column=1)




def createtable():
    rownum = int(textfiled1.get())
    columnnum = int(textfiled2.get())
    table = Tk(className="招标统计表")
    Label(master=table, text="投标对象").grid(row=0, column=0, padx=10, pady=12)
    # Button(master=table, text="平均分", command=getaverage).grid(row=0, column=int(columnnum)+1, padx=10, pady=12)
    Label(master=table, text="客观分").grid(row=0, column=int(columnnum)+2, padx=10, pady=12)
    # Button(master=table, text="总分").grid(row=0, column=int(columnnum)+3, padx=10, pady=12)
    for column in range(int(columnnum)):
        Label(master=table, text="评委"+str(column+1)).grid(row=0, column=column+1, padx=10, pady=12)

    itemlist = []
    totalnum = rownum * columnnum
    for num in range(totalnum):
        itemlist.append(num)
    for row in range(rownum):
        for column in range(columnnum):
            itemlist[row*columnnum+column] = Entry(master=table, width=5, justify=CENTER)
            itemlist[row*columnnum+column].grid(row=row+1, column=column+1, padx=10, pady=12)

    objectlist = []
    for num in range(rownum):
        objectlist.append(num)
    for row in range(rownum):
        objectlist[row] = Entry(master=table, justify=CENTER)
        objectlist[row].grid(row=row+1)

    objscorelist = []
    for num in range(rownum):
        objscorelist.append(num)
    for row in range(rownum):
        objscorelist[row] = Entry(master=table, width=5, justify=CENTER)
        objscorelist[row].grid(row=row+1, column=columnnum+2)

    def getaverage():
        averagescores=[]
        for i in range(rownum):
            averagescores.append(0)
        for row in range(rownum):
            for column in range(columnnum):
                averagescores[row] += int(Entry.get(itemlist[row*columnnum+column]))
            averagescores[row] /= columnnum
            averagescores[row] = '%.2f' %averagescores[row]
            averagescores[row] = float(averagescores[row])
        for row in range(rownum):
            Label(master=table, text=averagescores[row]).grid(row=row+1, column=columnnum+1)
        return averagescores

    def gettotal():
        averagesocres=getaverage()
        totalscores=[]
        for row in range(rownum):
            totalscores.append(0)
            totalscores[row]=averagesocres[row]+int(Entry.get(objscorelist[row]))
            Label(master=table, text=totalscores[row]).grid(row=row+1, column=columnnum+3)
        return totalscores

    def export2csv():
        rowlist1 = ['投标对象', '平均分', '客观分', '总分']
        for column in range(columnnum):
            rowlist1.insert(column+1,"评委"+str(column+1))
        averagescores = getaverage()
        totalscores = gettotal()
        rowlists =[rowlist1]
        for row in range(rownum):
            objectname = Entry.get(objectlist[row])
            averagescore = averagescores[row]
            totalscore = totalscores[row]
            objscore = Entry.get(objscorelist[row])
            rowlist = [objectname,averagescore,objscore,totalscore]
            for column in range(columnnum):
                rowlist.insert(column+1, Entry.get(itemlist[row*columnnum+column]))
            rowlists.append(rowlist)

        csv_file = open('statistical_table.csv', 'w', encoding="GB2312")
        writer = csv.writer(csv_file)
        for data in rowlists:
            writer.writerow(data)
        messagebox.showinfo(title='导出成功')




    Button(master=table, text="平均分", command=getaverage).grid(row=0, column=columnnum+1, padx=10, pady=12)
    Button(master=table, text="总分", command=gettotal).grid(row=0, column=columnnum+3, padx=10, pady=12)
    Button(master=table, text="保存").grid(row=rownum+1, column=1 )
    Button(master=table, text="导出",command=export2csv).grid(row=rownum+1, column=4)
    Button(master=table, text="退出", command=table.destroy).grid(row=rownum+1, column=7)
    table.mainloop()


Button(text='新建评分表', command=createtable).grid(row=2, column=0)
Button(text='退出', command=root.destroy).grid(row=2, column=1)

root.mainloop()
